package easy;

public class a283_移动零 {
    public void moveZeroes(int[] nums) {
        /*int temp = 0;
        if (nums.length != 0) {
            for (int s : nums) {
                for (int i = 0; i < nums.length; i++) {
                    if (i < nums.length - 1 && nums[i] == 0) {
                        temp = nums[i];
                        nums[i] = nums[i + 1];
                        nums[i + 1] = temp;
                    }
                }
            }
        }*/
        int leftZeroIndex;
        int zeroIndex = 0;
        while (zeroIndex < nums.length) {
            if (nums[zeroIndex] == 0) {
                break;
            }
            zeroIndex++;
        }
        if (zeroIndex == nums.length) return;
        leftZeroIndex = zeroIndex;
        while (zeroIndex < nums.length - 1) {
            if (nums[zeroIndex + 1] != 0) {
                nums[leftZeroIndex] = nums[zeroIndex + 1];
                nums[zeroIndex + 1] = 0;
                leftZeroIndex++;
            }
            zeroIndex++;
        }
    /*
    法二
     */
        int j = 0;
        for (int i=0;i<nums.length;i++){
            if(nums[i]!=0){
                nums[j]=nums[i];
                j++;
            }
        }
        while (j<nums.length){
            nums[j]=0;
        }
    }

}
